package com.twu.biblioteca;

import java.util.Scanner;

import com.twu.bean.User;

public class BibliotecaApp
{
	private static Scanner scanner;
	private static BibliotecaLibrary bibliotecaLibrary;

	public static void main(String[] args)
	{
		bibliotecaLibrary = new BibliotecaLibrary();
		scanner = new Scanner(System.in);
		System.out.println(bibliotecaLibrary.start());
		login();
		handleOption();
	}

	private static void login()
	{
		if (loginOrNot())
		{
			boolean isLogin = false;
			while (!isLogin)
			{
				isLogin = bibliotecaLibrary.isLogin(getInputUser());
				handleLogin(isLogin);
			}
		} else
		{
			bibliotecaLibrary.guest();
		}
	}

	private static void handleLogin(boolean login)
	{
		if (login)
		{
			bibliotecaLibrary.login();
		} else
		{
			System.out.println("username or password is incorrect, please input again.");
		}
	}

	private static boolean loginOrNot()
	{
		System.out.println("Do you want to login? Input y to login, others to no login:");
		return Login.loginOrNot(scanner.next());
	}

	private static User getInputUser()
	{
		System.out.print("username:");
		String username = scanner.next();
		System.out.print("password:");
		String password = scanner.next();
		return new User(username, password);
	}

	private static void handleOption()
	{
		while (true)
		{
			System.out.println(bibliotecaLibrary.getTips());
			String input = scanner.next();
			System.out.println(bibliotecaLibrary.executeMenuOption(input));
			System.out.println("\n\n-----------------------------");
		}
	}

}
